<template>
  <upload-excel :on-success="handleSuccess" />
</template>

<script>
import { importEmployee } from '@/api/employees'
import { formatExcelDate } from '@/utils'
export default {
  name: 'Import',
  methods: {
    async handleSuccess({ header, results }) {
      const data = this.transExcel(results)
      //   console.log(header, results)
      console.log(data)
      const res = await importEmployee(data)
      console.log(res)
      this.$message.success('就不恭喜你导入成功')

      this.$router.back()
    },
    transExcel(results) {
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      return results.map(item => {
        const obj = {}
        const zhKeys = Object.keys(item)
        zhKeys.forEach(zhitem => {
          const enkey = userRelations[zhitem]
          if (enkey === 'correctionTime' || enkey === 'timeOfEntry') {
            obj[enkey] = new Date(formatExcelDate(item[zhitem]))
          } else {
            obj[enkey] = item[zhitem]
          }
        })
        return obj
      })
    }
  }
}
</script>
